﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IAC_2013.com.iac.action;
using System.Data.SqlClient;
using DevComponents.DotNetBar.Controls;
using IAC_2013.com.iac.util;
using IAC_2013.com.iac.form;

namespace IAC_2013.Controls
{
    public partial class InvoiceIn : UserControl
    {
        DataTable tb;
        AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
        public InvoiceIn()
        {
            InitializeComponent();
            load_product();
            load_partner();
            load_warehouse();
            load_currency();
        }

        private MetroBillCommands _Commands;
        public MetroBillCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    MetroBillCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandChanged(oldValue, value);
                }
            }
        }

        public virtual void OnCommandChanged(MetroBillCommands oldValue, MetroBillCommands newValue)
        {
            if (newValue != null)
            {
                btBack.Command = newValue.InvoiceIn.Cancel;
            }
            else
            {
                btBack.Command = null;
            }
        }

        public void load_product()
        {
            tb = new DataTable();
            tb = InvoiceInAction.list_Product();
            (grvProduct.Columns["ma_sp"] as DataGridViewComboBoxExColumn).DataSource = tb;
            (grvProduct.Columns["ma_sp"] as DataGridViewComboBoxExColumn).ValueMember = "id_sp";
            (grvProduct.Columns["ma_sp"] as DataGridViewComboBoxExColumn).DisplayMember = "ma_sp";
        }

        public void load_partner()
        {
            tb = new DataTable();
            tb = PartnersAction.listPartner(0);
            cbPartner.DataSource = tb;
            cbPartner.DisplayMember = "ten_ncc";
            cbPartner.ValueMember = "id_ncc";
        }

        public void load_warehouse()
        {
            tb = new DataTable();
            tb = WareHouseAction.listWareHouse();
            cbWarhouse.DataSource = tb;
            cbWarhouse.DisplayMember = "ten_kho";
            cbWarhouse.ValueMember = "id_kho";
            
        }

        public void load_currency()
        {
            tb = new DataTable();
            CurrentcyForm frm = new CurrentcyForm();
            tb = new CurrentcyAction().listCurrentcy(frm);
            cbCurrency.DataSource = tb;
            cbCurrency.DisplayMember = "ma_lt";
            cbCurrency.ValueMember = "id_lt";
        }

        private void grvrProduct_EditControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBoxEx cb = e.Control as ComboBoxEx;
            if (cb != null)
            {
                cb.SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged);
                cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
            }
        }

        private void cb_SelectedIndexChanged(object sender, EventArgs e)
        {
            tb = new DataTable();
            ComboBoxEx cb = sender as ComboBoxEx;
            if (cb != null)
            {
                int productId = Int32.Parse(cb.SelectedValue.ToString());
                int r = grvProduct.CurrentCell.RowIndex;
                tb = InvoiceInAction.getProduct(productId);
                grvProduct.Rows[r].Cells[1].Value = tb.Rows[0]["ten_loai_sp"].ToString();
                grvProduct.Rows[r].Cells[2].Value = tb.Rows[0]["ten_sp"].ToString();
                grvProduct.Rows[r].Cells[3].Value = tb.Rows[0]["ten_dv"].ToString();
                grvProduct.Rows[r].Cells[4].Value = tb.Rows[0]["quy_cach_sp"].ToString();
                grvProduct.Rows[r].Cells[5].Value = tb.Rows[0]["kich_thuoc_sp"].ToString();
            }
        }

        private void btSave_Click(object sender, EventArgs e)
        {

        }

        private void cbPartnerCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            namesCollection = CommonUtils.autoCompleted("ncc_nha_cung_cap", "ten_ncc");
            cbPartner.AutoCompleteMode = AutoCompleteMode.Suggest;
            cbPartner.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbPartner.AutoCompleteCustomSource = namesCollection;
        }

        private void cbWarhouse_SelectedIndexChanged(object sender, EventArgs e)
        {
            namesCollection = CommonUtils.autoCompleted("kho_hang", "ten_kho");
            cbPartner.AutoCompleteMode = AutoCompleteMode.Suggest;
            cbPartner.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbPartner.AutoCompleteCustomSource = namesCollection;
        }

        private void cbCurrency_SelectedIndexChanged(object sender, EventArgs e)
        {
            namesCollection = CommonUtils.autoCompleted("lt_loai_tien", "ma_lt");
            cbPartner.AutoCompleteMode = AutoCompleteMode.Suggest;
            cbPartner.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbPartner.AutoCompleteCustomSource = namesCollection;
        }

        private void txtInvoiceNumber_TextChanged(object sender, EventArgs e)
        {
            namesCollection = CommonUtils.autoCompleted("pn_phieu_nhap", "so_pn");
            cbPartner.AutoCompleteMode = AutoCompleteMode.Suggest;
            cbPartner.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbPartner.AutoCompleteCustomSource = namesCollection;
        }

    }
}
